//
//  HMStatusRetweetedView.swift
//  新浪微博
//
//  Created by 饶鹏伟 on 16/4/12.
//  Copyright © 2016年 饶鹏伟. All rights reserved.
//

import UIKit
import SnapKit
//转发微博的视图
class HMStatusRetweetedView: UIView {
    
    
    //懒加载子视图
    private lazy var contentLabel: UILabel=UILabel(text: "我是转发微博", textColor: UIColor.darkGrayColor(), fontSize: 15, margin: StatusCellMargin)
    private lazy var pictureView: HMStatusPictureView=HMStatusPictureView()

    //转发微博底部约束条件
    var bottomCons: Constraint?
    
    //外界给statusViewModel这个属性赋值 会调用didSet方法
    var statusViewModel: HMStatusViewModel?{
        
        didSet{
            
           contentLabel.text=statusViewModel?.status?.retweeted_status?.text
            
            //卸载约束
            self.bottomCons?.uninstall()
            
            //判断是否有配图视图
            if let urls=statusViewModel?.status?.retweeted_status?.pic_urls where urls.count>0{
                
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomCons=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                //设置数据源
                pictureView.imageURLs=urls
                
                //显示配图信息
                pictureView.hidden=false
                
            }else{
                
               //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                self.bottomCons=make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
                //设置数据源
                pictureView.imageURLs=nil
                
                //显示配图信息
                pictureView.hidden=true
                
            }
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面的显示
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置转发微博的界面
    private func setupUI(){
        
       //设置背景色
        backgroundColor=UIColor(white: 0.9, alpha: 1)
        
        //添加子视图
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置contentLabel的约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
           make.top.left.equalTo(self).offset(StatusCellMargin)
           
            
        }
        
        //设置pictureView的约束
       
        pictureView.snp_makeConstraints { (make) -> Void in
            
           make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
           make.left.equalTo(contentLabel.snp_left)
            
        }
        
        //转发微博高度的自动计算 设置其底部的约束
        self.snp_makeConstraints { (make) -> Void in
            
        self.bottomCons=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            
        }
        
    }
    
    
    
}
